using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Systems.Workflow.Bases;

using Deepcode.Flux.Core.Services.State;
using Example.Workflow.Objects;

public partial class extensions_exampleworkflow_forms_ui_WFAddHistory : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the task that is manipulating a piece of data
	/// </summary>
	public int TaskID
	{
		get
		{
			object o = ViewState["TaskID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["TaskID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the exit path chosen
	/// </summary>
	public int ExitID
	{
		get
		{
			object o = ViewState["ExitID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["ExitID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.ExitID = GetAttribute("ExitID", -1);
			this.TaskID = GetAttribute("TaskID", -1);

			// Load the task and the chosen exit path
			Task t = new Tasks().GetTasksByID( TaskID );
			TaskExitPath te = new TaskExitPaths().GetTaskExitPathByID(ExitID);
			if( (t==null) || (te==null) ) GoBack();

			// Load the data
			TelephoneBookEntry entry = new TelephoneBook().GetTelephoneBookByID( t.FK_DataID );
			if( entry == null ) GoBack();

			fName.Text = entry.Name;
			fTel.Text = entry.Tel;
			fNotes.Text = entry.Notes.Replace("\n", "<br/>");
			fHistory.Text = entry.ApprovalHistory.Replace("\n", "<br/>");
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Return to the taskpad
	/// </summary>
	protected void GoBack()
	{
		Response.Redirect("~/admin/menu/taskpad.aspx", true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;

		// Load the task and the chosen exit path
		Task t = new Tasks().GetTasksByID( TaskID );
		TaskExitPath te = new TaskExitPaths().GetTaskExitPathByID(ExitID);
		if( (t==null) || (te==null) ) GoBack();

		// Load the data
		TelephoneBook book = new TelephoneBook();
		TelephoneBookEntry entry = book.GetTelephoneBookByID( t.FK_DataID );
		if( entry == null ) GoBack();
		
		entry.ApprovalHistory += "\n============================================";
		entry.ApprovalHistory += "\n" + System.DateTime.Now.ToString("r");
		entry.ApprovalHistory += "\n" + this.fNewNotes.Text;
		book.Save(entry);
		
		// Now, continue processing
		WorkflowEngine engine = new WorkflowEngine( t.HandlerCodeID );
		if( engine == null ) GoBack();
		engine.ProcessExitAction( this.TaskID, this.ExitID );
		// Redirect back to main form
		GoBack();
	}
	#endregion
}
